import axios from 'axios'
// import { Toast } from 'vant'

const service = axios.create({
  timeout: 5000,
  baseURL: 'http://175.27.243.236:7020'
})

service.interceptors.request.use(
  config => {
    return config
  },
  error => {
    console.log(error)
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  response => {
    const res = response
    if (res.status === 200) {
      return res
    } else {
      // const message = (res.error && res.error.message) || res.message || res.msg || '未知错误'
      // Toast.fail(message)
      console.log('拦截器打印错误:', res)
      return Promise.reject(
        new Error(res.message || (res.error && res.error.message) || '未知错误')
      )
    }
  },
  error => {
    console.log('服务器错误信息:', error)
    if (error.response) {
      const code = error.response.status
      if (code === 401) {
        return Promise.reject(new Error('登录过期,请重新登录'))
      }
      // Toast.fail(error)
      return Promise.reject(error.message)
    } else {
      return Promise.reject(error.message)
    }
  }
)
export default service
